package com.hmyd.hmyd_android.ui;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.adapter.LatestAdapter;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.LatestDto;
import com.hmyd.hmyd_android.dto.Page;
import com.hmyd.hmyd_android.utils.Utils;
import com.hmyd.hmyd_android.view.listview.XListView;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * 最新动态
 * 
 * @author wangc
 * 
 */
@ContentView(R.layout.activity_latest)
public class LatestActivity extends BasicListActivity implements OnItemClickListener {
	// 标题栏
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;

	@ViewInject(R.id.events_list)
	private XListView mListView;

	@ViewInject(R.id.list)
	private Button list;
	@ViewInject(R.id.medialist)
	private Button medialist;

	private LatestAdapter mAdapter;
	private List<LatestDto> datas = new ArrayList<LatestDto>();

	private final int REFRESH = 1;
	private final int LOADMORE = 2;
	protected boolean hasMore = false;
	private String cursorId;
	private int status = 0;

	@OnClick({ R.id.list, R.id.medialist })
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.list:
			datas.clear();
			onRequest(REFRESH, UrlConstant.DYNAMIC, HttpMethod.GET, null);
			list.setTextColor(getResources().getColor(R.color.text_orange));
			medialist.setTextColor(getResources().getColor(R.color.black));
			list.setBackgroundResource(R.drawable.ind_img_butxi);
			medialist.setBackgroundResource(R.drawable.ind_img_butxibai);
			status = 0;
			list.setEnabled(false);
			medialist.setEnabled(true);
			break;
		case R.id.medialist:
			datas.clear();
			onRequest(REFRESH, UrlConstant.MEDIALIST, HttpMethod.GET, null);
			medialist.setTextColor(getResources().getColor(R.color.text_orange));
			list.setTextColor(getResources().getColor(R.color.black));
			list.setBackgroundResource(R.drawable.ind_img_butxibai);
			medialist.setBackgroundResource(R.drawable.ind_img_butxi);
			status = 1;
			list.setEnabled(true);
			medialist.setEnabled(false);
			break;

		default:
			break;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
	}

	private void init() {
		mAdapter = new LatestAdapter(getApplicationContext(), datas);
		mListView.setPullRefreshEnable(true); // 允许下拉刷新
		mListView.setPullLoadEnable(false); // 禁止加载更多
		mListView.setAutoLoadEnable(false); // 禁止自动加载
		mListView.setXListViewListener(this);// 加载监听

		mListView.setOnItemClickListener(this);
		list.setEnabled(false);
		mListView.setAdapter(mAdapter);
	}

	@Override
	public void onPostSuccess(int postId, String result) {

		super.onPostSuccess(postId, result);
		Gson gson = new Gson();
		java.lang.reflect.Type type2 = new TypeToken<Page<LatestDto>>() {
		}.getType();
		Page<LatestDto> page = gson.fromJson(result, type2);
		hasMore = page.hasMore;
		if (page != null && page.records != null) {

			switch (postId) {
			case REFRESH:
				datas.clear();
				datas.addAll(page.records);
				break;
			case LOADMORE:
				datas.addAll(page.records);
				break;
			default:
				break;
			}

			if (datas.size() > 0) {
				cursorId = String.valueOf(datas.get(datas.size() - 1).createdTime);
			} else {
				cursorId = null;
			}

			mAdapter.notifyDataSetChanged();
		}

		if (getXList() != null && hasMore) {
			getXList().setPullLoadEnable(true);
		} else if (getXList() != null && !hasMore) {
			getXList().setPullLoadEnable(false);
		}
	}

	@Override
	public void onPostFailure(int postId, String msg) {
		if (getXList() != null && hasMore) {
			getXList().setPullLoadEnable(true);
		} else if (getXList() != null && !hasMore) {
			getXList().setPullLoadEnable(false);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	protected void setParams() {
		home_title.setOnTouchListener(touch);
		home_title_txt.setText("最新动态");
		onRequest(REFRESH, UrlConstant.DYNAMIC, HttpMethod.GET, null);
	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {

	}

	@Override
	public void onRefresh() {
		super.onRefresh();
		if (status == 0) {
			onRequest(REFRESH, UrlConstant.DYNAMIC, HttpMethod.GET, null);
		} else if (status == 1) {
			onRequest(REFRESH, UrlConstant.MEDIALIST, HttpMethod.GET, null);
		}
	}

	@Override
	public void onLoadMore() {
		super.onLoadMore();

		RequestParams params = new RequestParams();
		params.addBodyParameter("lastCursor", cursorId);

		if (status == 0) {
			onRequest(LOADMORE, UrlConstant.DYNAMIC, HttpMethod.GET, params);
		} else if (status == 1) {
			onRequest(LOADMORE, UrlConstant.MEDIALIST, HttpMethod.GET, params);
		}
	}

	public XListView getXList() {
		return mListView;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Utils.jumpURL(LatestActivity.this, datas.get(position - 1).url,
				datas.get(position - 1).title, datas.get(position - 1).url, false);
	}

}
